<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title id="pageTitle">赞赏名单 - 福利云API</title>
  <link rel="icon" type="image/png" sizes="56x56" href="FreeAPI.png" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />
  <link rel="stylesheet" href="css/style.css" />
    <style>
    .doc-back {
      display: inline-flex;
      align-items: center;
      gap: 8px;
      color: var(--primary);
      text-decoration: none;
      margin-bottom: 20px;
      font-weight: 500;
      transition: var(--transition);
      font-size: 1rem;
    }
    
    .doc-back:hover {
      text-decoration: underline;
      color: var(--secondary);
    }
    
    .doc-back i {
      font-size: 0.9rem;
    }
    
    .hero-container {
      display: flex;
      justify-content: center;
      align-items: flex-end;
      gap: 15px;
      margin: 30px 0;
      flex-wrap: wrap;
    }
     
     .hero-card {
       background: var(--card-bg);
       border-radius: var(--radius);
       overflow: hidden;
       box-shadow: var(--shadow);
       transition: var(--transition);
       text-align: center;
       padding: 15px;
       border: 1px solid var(--border-color);
       position: relative;
       cursor: pointer;
     }
     
     .hero-card:hover {
       transform: translateY(-5px);
     }
     
     .hero-card.first {
       order: 2;
       width: 160px;
       z-index: 3;
     }
     
     .hero-card.second {
       order: 1;
       width: 140px;
       z-index: 2;
     }
     
     .hero-card.third {
       order: 3;
       width: 130px;
       z-index: 1;
     }
     
     .hero-avatar-container {
       position: relative;
       display: inline-block;
       margin-bottom: 15px;
     }
     
     .hero-avatar {
       border-radius: 50%;
       object-fit: cover;
       border: 3px solid var(--primary);
       display: block;
     }
     
     .hero-card.first .hero-avatar {
       width: 80px;
       height: 80px;
     }
     
     .hero-card.second .hero-avatar {
       width: 75px;
       height: 75px;
     }
     
     .hero-card.third .hero-avatar {
       width: 70px;
       height: 70px;
     }
     
     .hero-name {
       font-size: 1.1rem;
       font-weight: bold;
       margin-bottom: 5px;
       color: var(--text-color);
     }
     
     .hero-amount {
       color: var(--primary);
       font-weight: bold;
     }
     
     .hero-medal {
       position: absolute;
       bottom: -5px;
       right: -5px;
       z-index: 10;
     }
     
     .hero-card.first .hero-medal {
       width: 46px;
       height: 46px;
     }
     
     .hero-card.second .hero-medal {
       width: 36px;
       height: 36px;
     }
     
     .hero-card.third .hero-medal {
       width: 32px;
       height: 32px;
     }
    
    .donor-container {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
      gap: 20px;
      margin: 30px 0;
    }
    
    .donor-card {
      background: var(--card-bg);
      border-radius: var(--radius);
      overflow: hidden;
      box-shadow: var(--shadow);
      transition: var(--transition);
      text-align: center;
      padding: 20px;
      border: 1px solid var(--border-color);
      position: relative;
      cursor: pointer;
    }
    
    .donor-card:hover {
      transform: translateY(-5px);
    }
    
    .donor-avatar {
      width: 80px;
      height: 80px;
      border-radius: 50%;
      margin: 0 auto 15px;
      object-fit: cover;
      border: 3px solid var(--primary);
    }
    
    .donor-name {
      font-size: 1.1rem;
      font-weight: bold;
      margin-bottom: 5px;
      color: var(--text-color);
    }
    
    .donor-amount {
      color: var(--primary);
      font-weight: bold;
    }
    
    .qrcode-container {
      display: flex;
      justify-content: center;
      align-items: center;
      margin: 30px auto;
      width: 100%;
      max-width: 30%;
    }
    
    .qrcode-item {
      text-align: center;
      background: var(--card-bg);
      border-radius: var(--radius);
      padding: 10px;
      box-shadow: var(--shadow);
      border: 1px solid var(--border-color);
      transition: var(--transition);
    }
     
    .qrcode-item:hover {
      transform: translateY(-5px);
    }
     
    .qrcode-img {
      width: 100%;
      height: auto;
      margin-bottom: 10px;
      border-radius: 8px;
    }
    
    .qrcode-title {
      font-size: 1rem;
      font-weight: bold;
      color: var(--primary);
      margin-top: 5px;
    }
    
    .header-box {
      background-color: var(--card-bg);
      border-radius: 8px;
      box-shadow: var(--shadow);
      margin-bottom: 30px;
      overflow: hidden;
      border: 1px solid var(--border-color);
    }
    
    .main-title {
      font-size: 2.2rem;
      color: var(--primary);
      text-align: center;
      padding: 25px 0;
      margin: 0;
    }
    
    .notice-banner {
      background-color: var(--card-bg);
      padding: 5px 20px 15px;
    }
    
    .notice-banner p {
      margin: 0;
      color: var(--gray);
      font-size: 1rem;
      line-height: 1.6;
      text-align: center;
    }
    
    .donate-title-container {
      text-align: center;
      margin: 30px 0;
    }
    
    .donate-title {
      font-size: 1.8rem;
      color: var(--primary);
      margin-bottom: 10px;
    }
    
    .donate-subtitle {
      color: var(--gray);
      font-size: 1rem;
      max-width: 600px;
      margin: 0 auto;
    }
    
    .section-title {
      font-size: 1.5rem;
      color: var(--primary);
      margin: 40px 0 20px;
      text-align: center;
      position: relative;
    }
    
    .section-title::after {
      content: '';
      display: block;
      width: 50px;
      height: 3px;
      background-color: var(--primary);
      margin: 10px auto 0;
    }
    
    /* 暗色主题样式 */
    [data-theme="dark"] .header-box {
      background-color: var(--card-bg);
      border: 1px solid var(--border-color);
    }
    
    [data-theme="dark"] .notice-banner {
      background-color: var(--card-bg);
    }
    
    [data-theme="dark"] .notice-banner p {
      color: var(--gray);
    }
    
    [data-theme="dark"] .main-title {
      color: var(--primary);
    }
    
    [data-theme="dark"] .section-title {
      color: var(--primary);
    }
    
    [data-theme="dark"] .hero-card,
    [data-theme="dark"] .donor-card,
    [data-theme="dark"] .qrcode-item {
      background-color: var(--card-bg);
      border: 1px solid var(--border-color);
    }
    
    [data-theme="dark"] .hero-name,
    [data-theme="dark"] .donor-name {
      color: var(--text-color);
    }
    
    [data-theme="dark"] .hero-amount,
    [data-theme="dark"] .donor-amount,
    [data-theme="dark"] .qrcode-title {
      color: var(--primary);
    }
    
    [data-theme="dark"] .hero-avatar,
    [data-theme="dark"] .donor-avatar {
      border-color: var(--primary);
    }
  </style>
  
  <!-- 主题切换脚本 -->
  <script src="js/theme.js?v=1"></script>
</head>
<body>

<div data-include="nav.html"></div>

  <!-- 页面主体内容 -->
  <div class="container">
    <a href="/" class="doc-back">
      <i class="fas fa-arrow-left"></i> 返回首页
    </a>

    <div class="header-box">
      <h1 class="main-title" id="donateTitle">赞赏名单</h1>
      <div class="notice-banner">
        <p id="donateSubtitle">感谢赞赏的人，因为你们，让我感受到更新更多API的动力。</p>
      </div>
    </div>
    


    <h2 class="section-title">赞赏英雄榜</h2>
    
    <div class="hero-container" id="heroContainer">
      <!-- 前三名赞赏英雄将通过JS动态加载 -->
      <div class="loader" id="heroLoader">
        <div class="loader-spinner"></div>
        <p>正在加载赞赏数据...</p>
      </div>
    </div>

    <h2 class="section-title">扫码上榜</h2>
    
    <div class="qrcode-container" id="qrcodeContainer">
      <!-- 二维码将通过JS动态加载 -->
    </div>
    
    <div class="tip-box">
      <div class="tip-title">
        <i class="fas fa-lightbulb"></i>
        <h3>请在备注中按照格式填写您的信息：</h3>
      </div>
      <p style="text-align: center; margin: 10px 0; color: var(--primary); font-weight: bold;">
        头像 介绍 名字 留空好评不留名的赞赏
      </p>
    </div>
    
    <div class="donor-container" id="donorContainer">
      <!-- 所有赞赏者卡片将通过JS动态加载 -->
      <div class="loader" id="loader">
        <div class="loader-spinner"></div>
        <p>正在加载赞赏数据...</p>
      </div>
    </div>
    

    <div data-include="footer.html"></div>
  </div>

  <!-- 浮动按钮 -->
  <div class="floating-buttons">
    <button id="theme-toggle" class="floating-btn" aria-label="切换主题">
      <i class="fas fa-moon"></i>
    </button>
    <button id="scroll-to-top" class="floating-btn" aria-label="返回顶部">
      <i class="fas fa-arrow-up"></i>
    </button>
  </div>
  
  <script>
    // 确保主题切换按钮有正确的点击事件
    document.addEventListener('DOMContentLoaded', function() {
      console.log('正在设置主题切换按钮的点击事件');
      const themeToggleBtn = document.getElementById('theme-toggle');
      if (themeToggleBtn) {
        console.log('找到主题切换按钮，添加点击事件');
        themeToggleBtn.addEventListener('click', function() {
          console.log('主题切换按钮被点击');
          if (typeof toggleTheme === 'function') {
            toggleTheme();
          } else {
            console.error('toggleTheme 函数未定义');
          }
        });
      } else {
        console.error('找不到主题切换按钮');
      }
    });
  </script>

  <!-- 脚本 -->
  <script src="js/toproll.js" defer></script>
  <script>
    async function loadIncludes() {
      const includes = document.querySelectorAll('[data-include]');
      for (const el of includes) {
        const url = el.getAttribute('data-include');
        try {
          const res = await fetch(url);
          if (res.ok) {
            const html = await res.text();
            el.innerHTML = html;
          } else {
            el.innerHTML = `<p style="color:red;">加载失败：${url}</p>`;
          }
        } catch {
          el.innerHTML = `<p style="color:red;">加载失败：${url}</p>`;
        }
      }
    }

    document.addEventListener('DOMContentLoaded', async () => {
      await loadIncludes();
      initializeTopNav();
      loadDonateData();
    });

    const heroContainer = document.getElementById('heroContainer');
    const donorContainer = document.getElementById('donorContainer');
    const qrcodeContainer = document.getElementById('qrcodeContainer');
    const heroLoader = document.getElementById('heroLoader');
    const loader = document.getElementById('loader');

    async function loadDonateData() {
      try {
        // 显示加载中
        if (heroLoader) heroLoader.style.display = 'block';
        if (loader) loader.style.display = 'block';
        
        // 加载赞赏数据
        const response = await fetch('config/donate.json');
        if (!response.ok) throw new Error(`服务器响应错误: ${response.status}`);
        
        const data = await response.json();
        
        // 更新标题和副标题
        if (data.title) document.getElementById('donateTitle').textContent = data.title;
        if (data.subtitle) document.getElementById('donateSubtitle').textContent = data.subtitle;
        
        // 渲染英雄榜（前三名）
        renderHeroes(data.donors || []);
        
        // 渲染所有赞赏者列表
        renderDonors(data.donors || []);
        
        // 渲染二维码
        renderQRCodes(data.qrcode || '');
        
      } catch (error) {
        console.error('加载赞赏数据失败:', error);
        const errorHtml = `
          <div class="error-panel" style="text-align: center;">
            <h2 class="error-title"><i class="fas fa-exclamation-triangle"></i> 加载数据失败</h2>
            <p>${error.message}</p>
            <div class="controls" style="margin-top: 20px;">
              <button class="btn" onclick="loadDonateData()">
                <i class="fas fa-redo"></i> 重试
              </button>
            </div>
          </div>
        `;
        
        heroContainer.innerHTML = errorHtml;
        donorContainer.innerHTML = errorHtml;
      } finally {
        // 隐藏加载中
        if (heroLoader) heroLoader.style.display = 'none';
        if (loader) loader.style.display = 'none';
      }
    }
    
    function renderHeroes(donors) {
      if (!donors.length) {
        heroContainer.innerHTML = `
          <div style="text-align: center; padding: 30px; width: 100%;">
            <i class="fas fa-info-circle" style="font-size: 2rem; color: var(--gray);"></i>
            <p style="margin-top: 10px; color: var(--gray);">暂无赞赏数据</p>
          </div>
        `;
        return;
      }
      
      // 按金额排序并取前三名
      const sortedDonors = [...donors].sort((a, b) => {
        // 提取数字部分
        const amountA = parseFloat(a.amount.replace(/,/g, '').replace(/元$/, ''));
        const amountB = parseFloat(b.amount.replace(/,/g, '').replace(/元$/, ''));
        return amountB - amountA; // 降序排列
      });
      
      const topThree = sortedDonors.slice(0, 3);
      
      // 重新排序：第一名在中间，第二名在左边，第三名在右边
      const orderedHeroes = [];
      
      // 确保有足够的赞赏者
      if (topThree.length >= 1) orderedHeroes[0] = topThree[0]; // 第一名
      if (topThree.length >= 2) orderedHeroes[1] = topThree[1]; // 第二名
      if (topThree.length >= 3) orderedHeroes[2] = topThree[2]; // 第三名
      
      // 填充缺失的位置
      while (orderedHeroes.length < 3) {
        orderedHeroes.push(null);
      }
      
      const heroCards = [];
      
      // 第一名（中间）
      if (orderedHeroes[0]) {
        heroCards.push(`
          <div class="hero-card first">
            <div class="hero-avatar-container">
              <img src="${orderedHeroes[0].avatar}" alt="${orderedHeroes[0].intro || orderedHeroes[0].name}" title="${orderedHeroes[0].intro || orderedHeroes[0].name}" class="hero-avatar" onerror="this.src='images/default-avatar.jpg'">
              <img src="img/01.png" class="hero-medal" alt="第一名" onerror="this.src='images/default-medal.jpg'">
            </div>
            <div class="hero-name">${orderedHeroes[0].name}</div>
            <div class="hero-amount">${orderedHeroes[0].amount}</div>
          </div>
        `);
      }
      
      // 第二名（左边）
      if (orderedHeroes[1]) {
        heroCards.push(`
          <div class="hero-card second">
            <div class="hero-avatar-container">
              <img src="${orderedHeroes[1].avatar}" alt="${orderedHeroes[1].intro || orderedHeroes[1].name}" title="${orderedHeroes[1].intro || orderedHeroes[1].name}" class="hero-avatar" onerror="this.src='images/default-avatar.jpg'">
              <img src="img/02.png" class="hero-medal" alt="第二名" onerror="this.src='images/default-medal.jpg'">
            </div>
            <div class="hero-name">${orderedHeroes[1].name}</div>
            <div class="hero-amount">${orderedHeroes[1].amount}</div>
          </div>
        `);
      }
      
      // 第三名（右边）
      if (orderedHeroes[2]) {
        heroCards.push(`
          <div class="hero-card third">
            <div class="hero-avatar-container">
              <img src="${orderedHeroes[2].avatar}" alt="${orderedHeroes[2].intro || orderedHeroes[2].name}" title="${orderedHeroes[2].intro || orderedHeroes[2].name}" class="hero-avatar" onerror="this.src='images/default-avatar.jpg'">
              <img src="img/03.png" class="hero-medal" alt="第三名" onerror="this.src='images/default-medal.jpg'">
            </div>
            <div class="hero-name">${orderedHeroes[2].name}</div>
            <div class="hero-amount">${orderedHeroes[2].amount}</div>
          </div>
        `);
      }
      
      heroContainer.innerHTML = heroCards.join('');
    }

    function renderDonors(donors) {
      if (!donors.length) {
        donorContainer.innerHTML = `
          <div style="grid-column: 1 / -1; text-align: center; padding: 30px;">
            <i class="fas fa-info-circle" style="font-size: 2rem; color: var(--gray);"></i>
            <p style="margin-top: 10px; color: var(--gray);">暂无赞赏数据</p>
          </div>
        `;
        return;
      }
      
      donorContainer.innerHTML = donors.map(donor => `
        <div class="donor-card">
          <img src="${donor.avatar}" alt="${donor.intro || donor.name}" title="${donor.intro || donor.name}" class="donor-avatar" onerror="this.src='images/default-avatar.jpg'">
          <div class="donor-name">${donor.name}</div>
          <div class="donor-amount">${donor.amount}</div>
        </div>
      `).join('');
    }

    function renderQRCodes(qrcode) {
      qrcodeContainer.innerHTML = '';
      
      if (qrcode) {
        qrcodeContainer.innerHTML = `
          <div class="qrcode-item" style="width: 100%; margin: 0 auto;">
            <img src="${qrcode}" alt="赞赏二维码" class="qrcode-img" style="width: 100%; height: auto; object-fit: contain;" onerror="this.src='images/default-qrcode.jpg'">
            <div class="qrcode-title">扫码赞赏</div>
          </div>
        `;
      } else {
        qrcodeContainer.innerHTML = `
          <div style="text-align: center; padding: 30px; width: 100%;">
            <i class="fas fa-info-circle" style="font-size: 2rem; color: var(--gray);"></i>
            <p style="margin-top: 10px; color: var(--gray);">暂无赞赏二维码</p>
          </div>
        `;
      }
    }
  </script>
</body>
</html>
